package com.example.hxds.mis.api.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.map.MapUtil;
import com.example.hxds.common.exception.HxdsException;
import com.example.hxds.common.util.PageUtils;
import com.example.hxds.common.util.R;
import com.example.hxds.mis.api.controller.form.*;
import com.example.hxds.mis.api.feign.*;
import com.example.hxds.mis.api.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    public OdrServiceApi odrServiceApi;

    @Autowired
    private CstServiceApi cstServiceApi;
    @Autowired
    private MpsServiceApi mpsServiceApi;
    @Autowired
    private RuleServiceApi ruleServiceApi;
    @Autowired
    private NebulaServiceApi nebulaServiceApi;
    @Autowired
    private DrServiceApi drServiceApi;

    @Override
    public PageUtils searchOrderByPage(SearchOrderByPageForm form) {
        R r = odrServiceApi.searchOrderByPage(form);
        HashMap map = (HashMap) r.get("result");
        PageUtils pageUtils = BeanUtil.toBean(map, PageUtils.class);
        return pageUtils;
    }


    //查询订单详情
    @Override
    public HashMap searchOrderComprehensiveInfo(long orderId) {
        HashMap map = new HashMap();

        SearchOrderContentForm searchOrderContentForm = new SearchOrderContentForm();
        searchOrderContentForm.setOrderId(orderId);
        R r = odrServiceApi.searchOrderContent(searchOrderContentForm);
        if (!r.containsKey("result")) {
            throw new HxdsException("不存在订单记录");
        }
        HashMap content = (HashMap) r.get("result");
        map.put("content", content);

        long customerId = MapUtil.getLong(content, "customerId");
        SearchCustomerBriefInfoForm searchCustomerBriefInfoForm = new SearchCustomerBriefInfoForm();
        searchCustomerBriefInfoForm.setCustomerId(customerId);
        r = cstServiceApi.searchCustomerBriefInfo(searchCustomerBriefInfoForm);
        HashMap customerInfo = (HashMap) r.get("result");
        map.put("customerInfo", customerInfo);

        long driverId = MapUtil.getLong(content, "driverId");
        SearchDriverBriefInfoForm searchDriverBriefInfoForm = new SearchDriverBriefInfoForm();
        searchDriverBriefInfoForm.setDriverId(driverId);
        r = drServiceApi.searchDriverBriefInfo(searchDriverBriefInfoForm);
        HashMap driverInfo = (HashMap) r.get("result");
        map.put("driverInfo", driverInfo);

        if (content.containsKey("chargeRuleId")) {
            long chargeRuleId = MapUtil.getLong(content, "chargeRuleId");
            SearchChargeRuleByIdForm searchChargeRuleByIdForm = new SearchChargeRuleByIdForm();
            searchChargeRuleByIdForm.setRuleId(chargeRuleId);
            r = ruleServiceApi.searchChargeRuleById(searchChargeRuleByIdForm);
            HashMap result = (HashMap) r.get("result");
            map.put("chargeRule", result);
        }

        if (content.containsKey("cancelRuleId")) {
            long cancelRuleId = MapUtil.getLong(content, "cancelRuleId");
            SearchCancelRuleByIdForm searchCancelRuleByIdForm = new SearchCancelRuleByIdForm();
            searchCancelRuleByIdForm.setRuleId(cancelRuleId);
            r = ruleServiceApi.searchCancelRuleById(searchCancelRuleByIdForm);
            HashMap result = (HashMap) r.get("result");
        }

        if (content.containsKey("profitsharingRuleId")) {
            long profitsharingRuleId = MapUtil.getLong(content, "profitsharingRuleId");
            SearchProfitsharingRuleByIdForm searchProfitsharingRuleByIdForm = new SearchProfitsharingRuleByIdForm();
            searchProfitsharingRuleByIdForm.setRuleId(profitsharingRuleId);
            r = ruleServiceApi.searchProfitsharingRuleById(searchProfitsharingRuleByIdForm);
            HashMap profitsharingRule = (HashMap) r.get("result");
            map.put("profitsharingRule", profitsharingRule);
        }

        CalculateDriveLineForm calculateDriveLineForm = new CalculateDriveLineForm();
        HashMap startPlaceLocation = (HashMap) content.get("startPlaceLocation");
        HashMap endPlaceLocation = (HashMap) content.get("endPlaceLocation");
        calculateDriveLineForm.setStartPlaceLatitude(MapUtil.getStr(startPlaceLocation, "latitude"));
        calculateDriveLineForm.setStartPlaceLongitude(MapUtil.getStr(startPlaceLocation, "longitude"));
        calculateDriveLineForm.setEndPlaceLatitude(MapUtil.getStr(endPlaceLocation, "latitude"));
        calculateDriveLineForm.setEndPlaceLongitude(MapUtil.getStr(endPlaceLocation, "longitude"));
        r = mpsServiceApi.calculateDriveLine(calculateDriveLineForm);
        HashMap driveLine = (HashMap) r.get("result");
        map.put("driveLine", driveLine);

        int status = MapUtil.getInt(content, "status");
        if (status >= 5 && status <= 8) {
            SearchOrderGpsForm form_8 = new SearchOrderGpsForm();
            form_8.setOrderId(orderId);
            r = nebulaServiceApi.searchOrderGps(form_8);
            ArrayList<HashMap> orderGps = (ArrayList<HashMap>) r.get("result");
            map.put("orderGps", orderGps);
        } else if (status == 4) {
            SearchOrderLastGpsForm form_9 = new SearchOrderLastGpsForm();
            form_9.setOrderId(orderId);
            r = nebulaServiceApi.searchOrderLastGps(form_9);
            HashMap lastGps = (HashMap) r.get("result");
            map.put("lastGps", lastGps);
        }
        return map;
    }

    @Override
    public HashMap searchOrderLastGps(SearchOrderLastGpsForm form) {
        SearchOrderStatusForm searchOrderStatusForm = new SearchOrderStatusForm();
        searchOrderStatusForm.setOrderId(form.getOrderId());
        //查询订单状态
        R r = odrServiceApi.searchOrderStatus(searchOrderStatusForm);
        if (!r.containsKey("result")) {
            throw new HxdsException("没有对应的订单记录");
        }
        int status = MapUtil.getInt(r, "result");
        if (status == 4) {
            //查询订单最后的GPS记录
            r = nebulaServiceApi.searchOrderLastGps(form);
            HashMap lastGps = (HashMap) r.get("result");
            return lastGps;
        } else {
            return null;
        }
    }

    @Override
    public ArrayList<HashMap> searchOrderStartLocationIn30Days() {
        R r = odrServiceApi.searchOrderStartLocationIn30Days();
        ArrayList<HashMap> list = (ArrayList<HashMap>) r.get("result");
        ArrayList<HashMap> result = new ArrayList<>();
        Map<HashMap, Integer> map = CollectionUtil.countMap(list);
        map.forEach((key, value) -> {
            key.replace("latitude", MapUtil.getDouble(key, "latitude"));
            key.replace("longitude", MapUtil.getDouble(key, "longitude"));
            key.put("count",value);
            result.add(key);
        });
        return result;
    }

 
}
